<%--
  Created by IntelliJ IDEA.
  User: FENG1
  Date: 2022/5/3
  Time: 15:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";%>
<html>
<head>
    <base href="<%=basePath%>">
    <title>Title</title>
    <link rel="stylesheet" href="static/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table" class="layui-hide"></table>
    </div>
</div>
</body>

<script type="text/html" id="user-toolbar">
    <shiro:hasPermission name="角色:delete">
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
        <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
    </shiro:hasPermission>
</script>


<script type="text/html" id="user-bar">
    <shiro:hasPermission name="角色:perass">
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="qx"><i class="layui-icon layui-icon-edit"></i>权限分配</button>
    </shiro:hasPermission>
    <shiro:hasPermission name="角色:update">
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</button>
    </shiro:hasPermission>
</script>

<script src="static/component/layui/layui.js"></script>
<script src="static/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let MODULE_PATH = "/system/user/";
        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '编号',
                    field: 'rid',
                    align: 'center'
                },
                {
                    title: '角色名称',
                    field: 'rName',
                    align: 'center'
                },
                {
                    title: '创建人',
                    field: 'rCreator',
                    align: 'center'
                },
                {
                    title: '创建时间',
                    field: 'rTime',
                    align: 'center'
                },
                {
                    title: '角色状态',
                    field: 'rState',
                    align: 'center'
                },
                {
                    title: '备注',
                    field: 'remark',
                    align: 'center'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    width: 200
                }
            ]
        ]
        //表格中的数据
        table.render({
            elem: '#user-table',
            url: 'role/All',
            cols: cols,
            page:false,
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });
        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'qx') {
                window.qx(obj);
            }
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(user-query)', function(data) {
            table.reload('user-table',{
                where: data.field
            })
            return false;
        });

        window.batchRemove = function(obj) {
            let data = table(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            let rids = "";
            for (let i = 0; i < data.length; i++) {
                rids += data[i].rid + ",";
            }
            rids = rids.substr(0, rids.length - 1);
            layer.confirm('确定要删除这些用户', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.post("/role/delRole",{rids:rids},function (result) {
                    layer.close(loading);
                    if (result.success) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 1000
                        }, function() {
                            table.reload('user-table');
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 2000
                        });
                    }
                })
            });
        }
        //添加一条角色信息
        window.add = function() {
            layer.open({
                type: 2,
                title: '新增角色',
                shade: 0.1,
                area: ['500px', '250px'],
                content: '/role/addRole'
            });
        }
        //给角色分配权限
        window.qx = function(obj) {
            let id = obj.data.rid;
            layer.open({
                type: 2,
                title: '权限分配',
                shade: 0.1,
                area: ['500px', '425px'],
                content: '/permission/per?id='+id
            });
        }
        //通过选中的角色id  查出来角色的内容进行展示
        window.edit = function(obj) {
            let id = obj.data.rid;
            layer.open({
                type: 2,
                title: '修改角色信息',
                shade: 0.1,
                area: ['500px', '500px'],
                content:'/role/selRole?id='+id
            });
        }
        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>